#remotes::install_github("braverock/FactorAnalytics", build_vignettes = TRUE, force = TRUE)
pacman::p_load(tidyverse,tidyquant,FFdownload,FactorAnalytics,PerformanceAnalytics)
install.packages("psych") # Install psych package
Error in install.packages : Updating loaded packages
library("psych") # Load psych package
Please remember to put your assignment solutions in rmd format using many chunks and putting readable text in between, similar to my examples given in Research Methods and Assignment 1!
For all exercises: Please use the Assignment-Forum to post your questions, I will try my best to help you along!
In this exercise we want to estimate the CAPM. Please read carefully through the two documents provided (right hand side: files). Then we start to collect the necessary data:
LS&P100I (S&P 100): total return index (RI) and market cap (MV)FFdownload). From both datasets we select data for the last (available) 60 months, calculate returns (simple percentage) for the US-Stocks and eliminate those stocks that have NAs for this period.mutate and map in combination with lm). Estimate the mean-return for each stock and plot the return/beta-combinations. Create the security market line and include it in the plot! What do you find?In this exercise we want to estimate the CAPM. Please read carefully through the two documents provided (right hand side: files). Then we start to collect the necessary data:
LS&P100I (S&P 100): total return index (RI) and market cap (MV)library(readxl)
sp100_daily_RI <- read_excel("sp100 daily RI_2.xlsx")
head(sp100_daily_RI, n=10)
library(readxl)
sp100_monthly_MV <- read_excel("sp100 monthly MV.xlsx")
head(sp100_monthly_MV, n=10)
sp100_monthly_MV <-
FFdownload). From both datasets we select data for the last (available) 60 months, calculate returns (simple percentage) for the US-Stocks and eliminate those stocks that have NAs for this period. tempf <- tempfile(fileext = ".RData"); tempd <- tempdir(); temptxt <- tempfile(fileext = ".txt")
inputlist <- c("F-F_Research_Data_Factors","F-F_Market_Beta")
FFdownload(exclude_daily=TRUE,tempdir=tempd,download=TRUE,download_only=FALSE,inputlist=inputlist)
Step 1: getting list of all the csv-zip-files!
Step 2: Downloading 2 zip-files
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/ME_Breakpoints_CSV.zip'
Step 3: Start processing 2 csv-files
|
| | 0%
|
|======================================================= | 50%
|
|==============================================================================================================| 100%
tempf2 <- tempfile(fileext = ".RData"); tempd2 <- tempdir()
FFdownload(output_file = tempf2,tempdir = tempd2,exclude_daily = TRUE, download = TRUE, download_only=FALSE, listsave=temptxt)
Step 1: getting list of all the csv-zip-files!
Step 2: Downloading 193 zip-files
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_weekly_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_5_Factors_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_ME_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_BE-ME_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_OP_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_INV_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_2x3_weekly_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_10x10_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_10x10_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_OP_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_OP_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_OP_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_OP_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_ME_OP_10x10_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_10x10_ME_OP_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_INV_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_INV_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_INV_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_INV_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_ME_INV_10x10_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_10x10_ME_INV_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_BEME_OP_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_BEME_OP_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_BEME_INV_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_BEME_INV_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_OP_INV_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_OP_INV_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_BEME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_BEME_OP_2x4x4_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_BEME_INV_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_BEME_INV_2x4x4_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_OP_INV_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_OP_INV_2x4x4_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_E-P_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_E-P_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_CF-P_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_CF-P_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_D-P_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_D-P_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_EP_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_EP_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_CFP_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_CFP_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_DP_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_DP_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Momentum_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Portfolios_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_ST_Reversal_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_Prior_1_0_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_Prior_1_0_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Portfolios_Prior_1_0_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_LT_Reversal_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_Prior_60_13_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_Prior_60_13_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Portfolios_Prior_60_13_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_AC_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_AC_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_BETA_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_BETA_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_NI_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_NI_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_VAR_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_VAR_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_RESVAR_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_RESVAR_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/5_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/5_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/12_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/12_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/17_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/17_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/30_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/30_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/38_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/38_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/49_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/49_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/ME_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/BE-ME_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/OP_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/INV_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/E-P_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/CF-P_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/D-P_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Prior_2-12_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_Mom_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_Mom_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_Mom_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_Mom_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_MOM_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_Mom_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_MOM_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_4_Portfolios_BE-ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_4_Portfolios_OP_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_4_Portfolios_BE-ME_INV_CSV.zip'
Step 3: Start processing 193 csv-files
|
| | 0%
|
|= | 1%
|
|== | 2%
|
|=== | 3%
NAs introduced by coercioninput string 15 is invalid in this locale
|
|==== | 4%
NAs introduced by coercion
|
|===== | 4%
NAs introduced by coercion
|
|===== | 5%
NAs introduced by coercion
|
|====== | 5%
input string 15 is invalid in this locale
|
|====== | 6%
|
|======= | 6%
|
|======= | 7%
|
|======== | 7%
|
|========= | 8%
|
|========== | 9%
|
|=========== | 10%
input string 16 is invalid in this localeinput string 16 is invalid in this locale
|
|============ | 11%
|
|============= | 11%
|
|============= | 12%
|
|============== | 12%
|
|============== | 13%
|
|=============== | 13%
input string 15 is invalid in this locale
|
|=============== | 14%
input string 15 is invalid in this locale
|
|================ | 15%
|
|================= | 15%
|
|================= | 16%
|
|================== | 16%
|
|================== | 17%
|
|=================== | 17%
input string 17 is invalid in this locale
|
|=================== | 18%
input string 17 is invalid in this locale
|
|==================== | 18%
|
|===================== | 19%
|
|====================== | 20%
input string 16 is invalid in this locale
|
|======================= | 21%
input string 16 is invalid in this localeinput string 17 is invalid in this locale
|
|======================== | 22%
input string 17 is invalid in this locale
|
|========================= | 22%
|
|========================= | 23%
|
|========================== | 23%
|
|========================== | 24%
|
|=========================== | 24%
|
|=========================== | 25%
|
|============================ | 25%
|
|============================ | 26%
|
|============================= | 26%
|
|============================== | 27%
|
|=============================== | 28%
|
|================================ | 29%
|
|================================ | 30%
|
|================================= | 30%
|
|================================== | 31%
|
|=================================== | 32%
input string 15 is invalid in this locale
|
|==================================== | 33%
input string 15 is invalid in this locale
|
|===================================== | 34%
|
|====================================== | 34%
|
|====================================== | 35%
|
|======================================= | 35%
|
|======================================= | 36%
|
|======================================== | 36%
|
|======================================== | 37%
|
|========================================= | 37%
|
|========================================== | 38%
|
|=========================================== | 39%
|
|============================================ | 40%
|
|============================================= | 41%
|
|============================================== | 41%
|
|============================================== | 42%
|
|=============================================== | 42%
|
|=============================================== | 43%
|
|================================================ | 44%
|
|================================================= | 45%
|
|================================================== | 45%
|
|================================================== | 46%
|
|=================================================== | 46%
|
|=================================================== | 47%
|
|==================================================== | 47%
|
|==================================================== | 48%
|
|===================================================== | 48%
|
|====================================================== | 49%
|
|======================================================= | 50%
|
|======================================================== | 51%
|
|========================================================= | 52%
|
|========================================================== | 52%
|
|========================================================== | 53%
|
|=========================================================== | 53%
|
|=========================================================== | 54%
|
|============================================================ | 54%
|
|============================================================ | 55%
|
|============================================================= | 55%
|
|============================================================== | 56%
|
|=============================================================== | 57%
|
|=============================================================== | 58%
|
|================================================================ | 58%
|
|================================================================ | 59%
|
|================================================================= | 59%
|
|================================================================== | 60%
|
|=================================================================== | 61%
|
|==================================================================== | 62%
|
|===================================================================== | 63%
|
|====================================================================== | 63%
|
|====================================================================== | 64%
|
|======================================================================= | 64%
|
|======================================================================= | 65%
|
|======================================================================== | 65%
|
|======================================================================== | 66%
|
|========================================================================= | 66%
|
|========================================================================== | 67%
|
|=========================================================================== | 68%
|
|============================================================================ | 69%
|
|============================================================================= | 70%
|
|============================================================================== | 70%
|
|============================================================================== | 71%
|
|=============================================================================== | 72%
|
|================================================================================ | 73%
|
|================================================================================= | 74%
|
|================================================================================== | 74%
|
|================================================================================== | 75%
|
|=================================================================================== | 75%
|
|=================================================================================== | 76%
|
|==================================================================================== | 76%
|
|==================================================================================== | 77%
|
|===================================================================================== | 77%
|
|===================================================================================== | 78%
|
|====================================================================================== | 78%
|
|======================================================================================= | 79%
|
|======================================================================================== | 80%
|
|========================================================================================= | 81%
|
|========================================================================================== | 82%
|
|=========================================================================================== | 82%
|
|=========================================================================================== | 83%
|
|============================================================================================ | 83%
|
|============================================================================================ | 84%
|
|============================================================================================= | 84%
|
|============================================================================================= | 85%
|
|============================================================================================== | 85%
|
|=============================================================================================== | 86%
|
|=============================================================================================== | 87%
|
|================================================================================================ | 87%
|
|================================================================================================ | 88%
|
|================================================================================================= | 88%
|
|================================================================================================= | 89%
|
|================================================================================================== | 89%
|
|=================================================================================================== | 90%
|
|==================================================================================================== | 91%
|
|===================================================================================================== | 92%
|
|====================================================================================================== | 93%
|
|======================================================================================================= | 93%
|
|======================================================================================================= | 94%
|
|======================================================================================================== | 94%
|
|======================================================================================================== | 95%
|
|========================================================================================================= | 95%
|
|========================================================================================================= | 96%
|
|========================================================================================================== | 96%
|
|=========================================================================================================== | 97%
input string 17 is invalid in this locale
|
|============================================================================================================ | 98%
input string 17 is invalid in this locale
|
|============================================================================================================= | 99%
|
|==============================================================================================================| 100%
load(tempf2)
ff<-FFdownload$x_Developed_ex_US_3_Factors$monthly$Temp2
sff<-ff["2015/2019"]
sff<-timetk::tk_tbl(ff)
colnames(sff)[1]<-"date"
sff
require(tidyquant)
require(timetk)
Loading required package: timetk
anyNA(sp100_daily_RI)
[1] FALSE
sp100_daily_RI_prices <- gather(sp100_daily_RI, key = symbol, value= prices, "AMAZON.COM":"CHARTER COMMS.CL.A")
anyNA(sp100_daily_RI_prices)
[1] FALSE
sp100_returns_RI_60_long <- sp100_daily_RI_prices %>% mutate(prices = as.numeric(prices)) %>% group_by(symbol) %>%
tq_transmute(select = prices,
mutate_fun = periodReturn,
period="monthly",
type="arithmetic",
col_rename = "Stock.returns") %>% ungroup() %>% mutate(date = as.yearmon(date))
anyNA(sp100_returns_RI_60_long)
[1] FALSE
sp100_returns_RI_60_long <- sp100_returns_RI_60_long[c(2,1,3)] %>% group_by(symbol)
fama_french <- sff %>%
select(date, Mkt.RF, RF) %>% mutate(date = as.yearmon(date))
mutate and map in combination with lm). Estimate the mean-return for each stock and plot the return/beta-combinations. Create the security market line and include it in the plot! What do you find? library(tidyquant)
library(tidyverse)
library(PerformanceAnalytics)
joined_data <- left_join(sp100_returns_RI_60_long, fama_french, by= c("date"))
joined_data <- mutate(joined_data,
monthly_ret_rf = Stock.returns - RF)
require(xts)
regr_fun <- function(data_xts) {
lm(monthly_ret_rf ~ Mkt.RF, data = as_data_frame(data_xts)) %>%
coef()
}
beta_alpha <- joined_data %>%
tq_mutate(mutate_fun = rollapply,
width = 60,
FUN = regr_fun,
by.column = FALSE,
col_rename = c("alpha", "beta"))
Problem with `mutate()` input `nested.col`.
ℹ `as_data_frame()` is deprecated as of tibble 2.0.0.
Please use `as_tibble()` instead.
The signature and semantics have changed, see `?as_tibble`.
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.
ℹ Input `nested.col` is `purrr::map(...)`.
ℹ The error occurred in group 1: symbol = "3M".`as_data_frame()` is deprecated as of tibble 2.0.0.
Please use `as_tibble()` instead.
The signature and semantics have changed, see `?as_tibble`.
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.
beta_alpha
beta_alpha_filter <- filter(beta_alpha, !is.na(alpha))
symbol_beta_alpha <- beta_alpha_filter %>%
select(symbol, alpha, beta)
symbol_beta_alpha
alpha <- beta_alpha %>%
select(symbol, alpha)
beta <- beta_alpha_filter %>%
select(symbol, beta)
beta
library(dplyr)
means_sp100_RI_60 <- joined_data %>%
group_by(symbol) %>%
summarize(mu = mean(monthly_ret_rf, na.rm=TRUE))
means_sp100_RI_60
mu.hat <- mutate(beta_alpha,
mu_capm = beta * mean(Mkt.RF))
mu.hat <- filter(mu.hat, !is.na(alpha))
mu.hat <- mu.hat %>%
select(symbol, alpha, beta, mu_capm)
mu.hat <- merge(mu.hat, means_sp100_RI_60)
sml.fit <- lm(mu_capm~beta, mu.hat)
install.packages("plotly")
Error in install.packages : Updating loaded packages
library(plotly)
p <- plot_ly(mu.hat, x = ~beta, y = ~mu_capm, type = 'scatter', mode = 'line', text = ~paste('symbol:', symbol)) %>%
add_markers(x = ~beta, y = ~mu)
p
sp100_monthly_MV <- read_excel("sp100 monthly MV.xlsx")
head(sp100_monthly_MV, n=10)
anyNA(sp100_monthly_MV)
[1] FALSE
sp100_monthly_MV <- gather(sp100_monthly_MV, key = symbol, value= value, "AMAZON.COM":"CHARTER COMMS.CL.A")
anyNA(sp100_daily_RI_prices)
[1] FALSE
mean value
mean_sp100_MV <- sp100_monthly_MV %>%
group_by(symbol) %>%
summarize(mean_value = mean(value, na.rm=TRUE))
Read Chapter 24 of our book. In this exercise use a Minimum Variance and a Tangecy (Maxium Sharpe Ratio) portfolio calculate from your stocks, as well as the S&P500 as a benchmark (Period 2000-01-01 - 2020-01-11). For all three Investment Opportunities imagine you invest 100USD per month into the portfolio. What is the overall return this investment provides you? How much should you have invested at the beginning (one-time investment) to get the exact same overall wealth at the end of 2020? Can you plot both wealth developments over time?
First of all, I am going to download all the crucial data, which we need in order to create the three portfolios. My personal stock choice are Apple, Nividia, Microsoft, American Express, Walmart, Bank of America, Morgan Stanley, Disney, Exxon Mobile. As the benchmark we take the S&P500, regarding to the underling task.
install.packages("FinCal",dependencies=TRUE)
Installing package into ‘/home/fkoeffel/R/x86_64-pc-linux-gnu-library/4.0’
(as ‘lib’ is unspecified)
trying URL 'https://cloud.r-project.org/src/contrib/FinCal_0.6.3.tar.gz'
Content type 'application/x-gzip' length 27972 bytes (27 KB)
==================================================
downloaded 27 KB
* installing *source* package ‘FinCal’ ...
** package ‘FinCal’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (FinCal)
The downloaded source packages are in
‘/tmp/RtmpVJFCX3/downloaded_packages’
library(timetk,PortfolioAnalytics)
SP500 <- tq_index("SP500")
Getting holdings for SP500
stock_names <- c("AAPL", "NVDA", "MSFT", "AXP", "WMT", "BAC", "GS","MS", "DIS", "XOM")
stock_returns <-tq_get(x = stock_names,get = "stock.prices", from = "2000-01-01", to = "2020-11-18") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly")
unique(stock_returns$symbol)
[1] "AAPL" "NVDA" "MSFT" "AXP" "WMT" "BAC" "GS" "MS" "DIS" "XOM"
stock_returns_xts <- stock_returns %>%
subset( select = c(symbol,date, monthly.returns)) %>%
pivot_wider(names_from = symbol,
values_from = monthly.returns) %>%
tk_xts(date_var = date, silent = TRUE)
stock_returns_xts
AAPL NVDA MSFT AXP WMT BAC GS MS DIS XOM
2000-01-31 -7.314352e-02 -0.2082775529 -0.160322068 0.0492545744 -1.805424e-01 0.000000000 0.0389327342 -0.0154069879 0.215481306 0.0582605562
2000-02-29 1.048197e-01 0.7268124024 -0.086845290 -0.1855081971 -1.095892e-01 -0.050322187 0.0095496407 0.0632075243 -0.063683351 -0.0861579935
2000-03-31 1.848407e-01 0.3200681837 0.188811586 0.1099207672 1.604329e-01 0.152472422 0.1378378128 0.1765744028 0.213235375 0.0356841158
2000-04-28 -8.651603e-02 0.0549292407 -0.343529450 0.0055061929 -1.991172e-02 -0.065554285 -0.1128790850 -0.0717486154 0.057575947 -0.0040058103
2000-05-31 -3.229218e-01 0.2805045374 -0.103046546 0.0836120712 4.063230e-02 0.141769366 -0.2111256487 -0.0684042450 -0.032951556 0.0782259961
2000-06-30 2.470245e-01 0.1139101096 0.278720774 -0.0347217980 1.091976e-03 -0.224352246 0.2884447099 0.1643356276 -0.079999876 -0.0577643909
2000-07-31 -2.983288e-02 -0.0560473493 -0.127343421 0.0891935082 -4.121459e-02 0.101744795 0.0424553386 0.0986815018 -0.006441462 0.0214972953
2000-08-31 1.992609e-01 0.3229168581 0.000000000 0.0410688866 -1.380091e-01 0.142161317 0.3122228219 0.1789381657 0.010130037 0.0236777578
2000-09-29 -5.774358e-01 0.0314960824 -0.136079022 0.0293884718 1.162282e-02 -0.022455210 -0.1192085574 -0.1500359735 -0.018051008 0.0914827064
2000-10-31 -2.402909e-01 -0.2410306953 0.141969053 -0.0110589762 -5.714276e-02 -0.082339130 -0.1238587521 -0.1197516375 -0.063725166 0.0008772319
2000-11-30 -1.565491e-01 -0.3482522808 -0.166969068 -0.0843752750 1.501376e-01 -0.157399734 -0.1772068641 -0.2108947362 -0.191972105 -0.0085035101
2000-12-29 -9.848479e-02 -0.1909722966 -0.244008658 0.0000000000 1.923796e-02 0.148670196 0.3021303840 0.2504926647 0.006890354 -0.0120738732
2001-01-31 4.537819e-01 0.5755841204 0.407780419 -0.1413408492 6.917641e-02 0.173187413 0.0648672154 0.0730230126 0.052267852 -0.0320638467
2001-02-28 -1.560695e-01 -0.1343824607 -0.033776377 -0.0683652135 -1.181338e-01 -0.047600968 -0.1934065859 -0.2319578358 0.016420305 -0.0215814636
2001-03-30 2.093140e-01 0.4527974233 -0.073093738 -0.0587962033 9.661250e-03 0.079881659 -0.0724794821 -0.1785658601 -0.075928994 -0.0006168926
2001-04-30 1.549600e-01 0.2830802652 0.238857380 0.0297704462 2.455438e-02 0.022831235 0.0718219781 0.1791415876 0.057692433 0.0938267982
2001-05-31 -2.173384e-01 0.0277311388 0.021107214 -0.0075394614 1.934157e-04 0.068071214 0.0439073947 0.0353559084 0.045289433 0.0066298558
2001-06-29 1.654135e-01 0.0834015312 0.055217955 -0.0788227168 -5.565195e-02 0.013164964 -0.0977919307 -0.0119974229 -0.086337989 -0.0155529996
2001-07-31 -1.918276e-01 -0.1277627025 -0.093287516 0.0415421457 1.454919e-01 0.059803111 -0.0293927057 -0.0652874736 -0.087919697 -0.0438465618
2001-08-31 -1.277255e-02 0.0470949713 -0.138087385 -0.0969505377 -1.404295e-01 -0.033323084 -0.0367962342 -0.1081582019 -0.034914534 -0.0331625351
2001-09-28 -1.638835e-01 -0.3514341971 -0.103067343 -0.2020868122 3.177000e-02 -0.041738331 -0.1092393068 -0.1312089320 -0.267793965 -0.0186801212
2001-10-31 1.321740e-01 0.5602478161 0.136407908 0.0155336371 3.838356e-02 0.010102439 0.0970810940 0.0606327228 -0.001610972 0.0012691169
2001-11-30 2.129844e-01 0.2748480770 0.104213304 0.1182466610 7.295765e-02 0.040515930 0.1386898810 0.1345054370 0.101129638 -0.0464083317
2001-12-31 2.816707e-02 0.2243780893 0.031770714 0.0844727476 4.482824e-02 0.035544881 0.0421345566 0.0079275099 0.022954316 0.0508030426
2002-01-31 1.287677e-01 -0.0173395870 -0.038339521 0.0067395638 4.222470e-02 0.001270886 -0.0609382467 -0.0128857039 0.016409255 -0.0063617223
2002-02-28 -1.221664e-01 -0.2240643478 -0.084287865 0.0167365546 3.384383e-02 0.024252785 -0.0694419664 -0.1069095645 0.092117751 0.0639055276
2002-03-28 9.078214e-02 -0.1303667892 0.033767162 0.1237306341 -1.027972e-02 0.063643499 0.1150238016 0.1667351924 0.003478073 0.0612592070
2002-04-30 2.534680e-02 -0.2152839920 -0.133476907 0.0032348764 -8.874387e-02 0.065569238 -0.1260656035 -0.1639497214 0.004332757 -0.0835044081
2002-05-31 -3.996440e-02 -0.0387820572 -0.025832365 0.0365760305 -3.150740e-02 0.045943782 -0.0419050830 -0.0473598043 -0.011648025 -0.0002272297
2002-06-28 -2.394848e-01 -0.4865511192 0.074444739 -0.1456129652 1.812497e-02 -0.064344881 -0.0278327273 -0.0523533250 -0.175032716 0.0247932736
2002-07-31 -1.388287e-01 -0.3556458710 -0.122851742 -0.0270878837 -1.059811e-01 -0.054860956 -0.0009969293 -0.0581428084 -0.061904705 -0.1016619836
2002-08-30 -3.341868e-02 -0.0876243062 0.022926452 0.0226889729 8.743421e-02 0.053834539 0.0567330319 0.0587359453 -0.115623369 -0.0294354855
2002-09-30 -1.694983e-02 -0.1524753486 -0.108801856 -0.1353302983 -7.800754e-02 -0.081401372 -0.1457960480 -0.2069285568 -0.034438691 -0.1001411600
2002-10-31 1.082776e-01 0.3901872061 0.222450705 0.1693120553 8.753062e-02 0.094044103 0.0861109804 0.1561874422 0.103038437 0.0551724534
2002-11-29 -3.547284e-02 0.4394961517 0.078735574 0.0703875971 6.536121e-03 0.004011125 0.1015361285 0.1623847680 0.186826254 0.0406858602
2002-12-31 -7.548030e-02 -0.3280795597 -0.103675144 -0.0899186284 -6.151254e-02 0.001976476 -0.1350574980 -0.1175952434 -0.166825730 0.0040226370
2003-01-31 2.091764e-03 -0.1033883016 -0.082012196 0.0050924491 -5.365329e-02 0.006899265 0.0016714249 -0.0455217631 0.072961429 -0.0226104463
2003-02-28 4.526188e-02 0.2228677362 0.001947463 -0.0548837806 5.439728e-03 -0.011563407 0.0198233169 -0.0277041428 -0.025142873 0.0029523777
2003-03-31 -5.796122e-02 0.0206019688 0.021518928 -0.0104221165 8.447313e-02 -0.025496811 -0.0197259117 0.0407052716 -0.002344580 0.0273367791
2003-04-30 5.660727e-03 0.1125779091 0.056175176 0.1420491250 8.245250e-02 0.107869413 0.1165966448 0.1732451571 0.096357024 0.0071531483
2003-05-30 2.623059e-01 0.8262384120 -0.037544232 0.1003691092 -6.587383e-02 0.002025844 0.0737808596 0.0223466910 0.053054634 0.0414583299
2003-06-30 6.183800e-02 -0.1245697371 0.041853399 0.0036013669 2.181836e-02 0.074174696 0.0276083289 -0.0655742966 0.005089225 -0.0134615445
2003-07-31 1.059799e-01 -0.1667395666 0.030030911 0.0589776650 4.173695e-02 0.044792934 0.0435320136 0.1151208801 0.109873332 -0.0091899208
2003-08-29 7.258242e-02 -0.0481926805 0.004165050 0.0199238798 5.830799e-02 -0.040208177 0.0154923551 0.0284571510 -0.064780867 0.0668878762
2003-09-30 -8.359120e-02 -0.1210784777 0.048265373 0.0002220140 -5.610964e-02 -0.005249072 -0.0518700003 0.0342288855 -0.016097385 -0.0291778064
2003-10-31 1.047288e-01 0.1070755106 -0.054436948 0.0438161617 5.717365e-02 -0.029599729 0.1224343807 0.0920472324 0.122458843 -0.0005464076
2003-11-28 -8.650016e-02 0.2007918071 -0.016449425 -0.0259958321 -5.615007e-02 -0.003962135 0.0232164786 0.0074719074 0.019876306 -0.0034527747
2003-12-31 2.199742e-02 0.0927932025 0.064565907 0.0573105028 -4.487609e-02 0.077538658 0.0275808763 0.0468524305 0.020000013 0.1325969020
2004-01-30 5.568725e-02 -0.0409483613 0.010230338 0.0748494163 1.508006e-02 0.012806576 0.0108155532 0.0102473615 0.028718393 -0.0051215534
2004-02-27 6.028378e-02 0.0000000000 -0.040506783 0.0304779284 1.060356e-01 0.005646718 0.0634854229 0.0266276576 0.105416421 0.0402515610
2004-03-31 1.304329e-01 0.1865171518 -0.060309001 -0.0275157876 4.435095e-03 -0.001795191 -0.0143573579 -0.0411648706 -0.058047395 -0.0137541076
2004-04-30 -4.659764e-02 -0.2227275558 0.048134832 -0.0559309497 -4.506635e-02 -0.006050557 -0.0705490323 -0.0991624211 -0.078431575 0.0230825597
2004-05-28 8.844078e-02 0.1427876973 0.003827117 0.0357508070 -1.997341e-02 0.032799031 -0.0293538495 0.0412532229 0.019105608 0.0230198373
2004-06-30 1.596589e-01 -0.1270790136 0.088829499 0.0153925271 -5.795758e-02 0.027825361 0.0026621928 -0.0138294023 0.086067232 0.0268212461
2004-07-30 -6.146531e-03 -0.2467023154 -0.002450923 -0.0219933184 9.713949e-03 0.004609075 -0.0607271036 -0.0605816570 -0.094154382 0.0425578145
2004-08-31 6.648183e-02 -0.1919585004 -0.038942003 -0.0045765254 -4.058112e-03 0.058228103 0.0165553366 0.0283802827 -0.027717903 0.0016518932
2004-09-30 1.235137e-01 0.1653288849 0.012820205 0.0287885647 1.006185e-02 -0.026947956 0.0400439848 -0.0281884132 0.004454551 0.0483732546
2004-10-29 3.522588e-01 -0.0034432854 0.011573170 0.0336374065 1.353411e-02 0.033695242 0.0579911136 0.0415670531 0.118403553 0.0184144659
2004-11-30 2.795788e-01 0.3220454717 0.068317272 0.0497455376 -3.449514e-02 0.033042862 0.0648502761 -0.0066550492 0.065820578 0.0468774309
2004-12-31 -3.952241e-02 0.2315733885 -0.003356976 0.0118470730 1.706940e-02 0.025535037 -0.0068726845 0.0939899544 0.043467477 0.0001948854
2005-01-31 1.940991e-01 -0.0271647781 -0.016466861 -0.0515077523 -7.951521e-03 -0.013194514 0.0391332528 0.0128571658 0.029856153 0.0066332570
2005-02-28 1.667104e-01 0.2648342221 -0.039664831 0.0149951541 -1.507612e-02 0.006038096 0.0088087346 0.0091135222 -0.024100855 0.2329470452
2005-03-31 -7.110979e-02 -0.1804070026 -0.039347601 -0.0490976358 -2.620202e-02 -0.045539054 0.0109373433 0.0138124865 0.028275136 -0.0586004610
2005-04-29 -1.346295e-01 -0.0770202776 0.046751862 0.0258908997 -5.926938e-02 0.021315422 -0.0267790544 -0.0763179006 -0.081099820 -0.0431207053
2005-05-31 1.026064e-01 0.2357500314 0.022998061 0.0218220145 5.116473e-03 0.028418626 -0.0869931181 -0.0695557213 0.039393750 -0.0095347516
2005-06-30 -7.419491e-02 -0.0140221002 -0.037209956 -0.0093121369 2.053797e-02 -0.005668223 0.0463589790 0.0716908364 -0.082361517 0.0225974933
2005-07-29 1.586524e-01 0.0127249317 0.030998718 0.0332519835 2.385887e-02 -0.044069432 0.0559574675 0.0161814711 0.018268439 0.0222725820
2005-08-31 9.941373e-02 0.1337765828 0.072284489 0.0043634282 -8.607434e-02 -0.001489565 0.0344249434 -0.0410929714 -0.017550638 0.0245713095
2005-09-30 1.433143e-01 0.1173401482 -0.060262372 0.0398265980 -2.535583e-02 -0.021612662 0.0935424514 0.0603493667 -0.042080220 0.0607679911
2005-10-31 7.423997e-02 -0.0212948497 -0.001166125 -0.0077038987 7.964358e-02 0.038954877 0.0415462205 0.0138750707 0.009946048 -0.1164616746
2005-11-30 1.776349e-01 0.0774957852 0.080200161 0.0331523783 2.642193e-02 0.060489103 0.0204954559 0.0297739625 0.022979182 0.0389422636
2005-12-30 6.001221e-02 0.0113419769 -0.055274721 0.0007780125 -3.331240e-02 0.005665625 -0.0096930819 0.0126717359 -0.028287050 -0.0320528729
2006-01-31 5.035437e-02 0.2297588779 0.076481506 0.0215696403 -1.474421e-02 -0.041603304 0.1080917772 0.0879705227 0.055903153 0.1171444907
2006-02-28 -9.296804e-02 0.0482652508 -0.042235949 0.0272639974 -1.626516e-02 0.036625872 0.0002833670 -0.0291294597 0.105887039 -0.0488376890
2006-03-31 -8.424539e-02 0.2149375831 0.012653781 -0.0246844488 4.530237e-02 0.004190020 0.1109060725 0.0529669483 -0.003572731 0.0250968985
2006-04-28 1.222891e-01 0.0206076083 -0.112458709 0.0262909352 -4.678200e-02 0.096179172 0.0233400637 0.0278828767 0.002509827 0.0364771658
2006-05-31 -1.508737e-01 -0.2135522014 -0.058454615 0.0102210379 7.972271e-02 -0.020390999 -0.0582689359 -0.0727838615 0.090844210 -0.0295437087
2006-06-30 -4.182706e-02 -0.0735425013 0.028697486 -0.0209713137 -5.779787e-03 -0.006198127 -0.0034448820 0.0602143904 -0.016393525 0.0072236180
2006-07-31 1.866602e-01 0.0399250463 0.032618127 -0.0190544004 -7.618832e-02 0.071309433 0.0178764979 0.0565111450 -0.010333305 0.1041564907
2006-08-31 -1.618857e-03 0.3148146729 0.072096314 0.0092204184 8.748122e-03 0.009648522 -0.0268414522 -0.0106765665 -0.001347084 0.0036015884
2006-09-29 1.345618e-01 0.0164889559 0.064202660 0.0673770268 1.028625e-01 0.040800684 0.1380429071 0.1082227330 0.042495659 -0.0084232991
2006-10-31 5.326018e-02 0.1784390426 0.049725010 0.0336182512 -8.111153e-04 0.005600147 0.1240392082 0.0520662442 0.017793648 0.0643816045
2006-11-30 1.304890e-01 0.0607972171 0.026136966 0.0157412784 -6.452922e-02 0.010050969 0.0263977605 -0.0035324632 0.050540378 0.0801315340
2006-12-29 -7.440583e-02 0.0005405783 0.017029899 0.0332080397 5.435004e-03 -0.008541912 0.0233571450 0.0691963384 0.046304537 -0.0023434810
2007-01-31 1.049061e-02 -0.1718454744 0.033489384 -0.0380039041 3.269863e-02 -0.015171250 0.0660383774 0.0200950346 0.026262152 -0.0330155474
2007-02-28 -1.306434e-02 0.0114189307 -0.084002966 -0.0231880365 1.300019e-02 -0.022507346 -0.0490665940 -0.0950599741 -0.026158605 -0.0285392022
2007-03-30 9.809725e-02 -0.0716127837 -0.010649153 -0.0082642018 -2.349924e-02 0.003737864 0.0241879461 0.0512549453 0.005255426 0.0525949188
2007-04-30 7.415782e-02 0.1428081061 0.074273446 0.0786054216 2.066011e-02 -0.002352039 0.0596692377 0.0702578785 0.015974057 0.0520876472
2007-05-31 2.143286e-01 0.0532072983 0.028370640 0.0710399995 -2.067464e-03 0.007286781 0.0558530987 0.0122604135 0.013150644 0.0522681198
2007-06-29 7.013793e-03 0.1925520427 -0.039752161 -0.0584793586 1.071463e-02 -0.035890502 -0.0609564852 -0.0136407347 -0.023499173 0.0085368147
2007-07-31 7.964569e-02 0.1077219513 -0.016287937 -0.0408230867 -4.489722e-02 -0.030067674 -0.1295112044 -0.0791944536 -0.033391669 0.0149023877
2007-08-31 5.100211e-02 0.1180074891 -0.005495210 0.0013665712 -4.569837e-02 0.068747762 -0.0654666105 -0.0234853064 0.018181867 0.0110742041
2007-09-28 1.082462e-01 0.0625488406 0.025409117 0.0127943684 4.580856e-04 0.004495467 0.2314071673 0.0101010616 0.023511773 0.0796685801
2007-10-31 2.377015e-01 -0.0237310223 0.249491200 0.0291595314 3.573870e-02 -0.039586083 0.1456397649 0.0718076846 0.006978882 -0.0061582160
2007-11-30 -4.069488e-02 -0.1085358178 -0.084186866 -0.0323218099 5.950039e-02 -0.044531780 -0.0858340222 -0.2161762421 -0.042737655 -0.0269964786
2007-12-31 8.703747e-02 0.0786303743 0.059523989 -0.1180059017 -3.252297e-03 -0.092582252 -0.0511382152 0.0073978346 -0.015725440 0.0508080414
2008-01-31 -3.166398e-01 -0.2771899902 -0.084270035 -0.0522762065 6.753662e-02 0.070043671 -0.0704427160 -0.0655472993 -0.075588654 -0.0852814444
2008-02-29 -7.638828e-02 -0.1301341727 -0.162401958 -0.1390189999 -2.266443e-02 -0.099886374 -0.1499376473 -0.1463316644 0.086126237 0.0196679635
2008-03-31 1.478160e-01 -0.0748008075 0.043382664 0.0335698381 6.736632e-02 -0.030042114 -0.0249953938 0.0849954034 -0.031780453 -0.0279277657
2008-04-30 2.121950e-01 0.0384026054 0.004932706 0.1026239402 1.006073e-01 -0.009760321 0.1592317666 0.0695212097 0.033461039 0.1003783371
[ reached getOption("max.print") -- omitted 151 rows ]
port <- portfolio.spec(assets=stock_names)%>%
add.constraint(type="full_investment") %>% #In our approach we use full_investment as our task it is
add.constraint(type="long_only") %>% #We do not take any short-selling
add.objective(type="return", name="mean")
port
**************************************************
PortfolioAnalytics Portfolio Specification
**************************************************
Call:
portfolio.spec(assets = stock_names)
Number of assets: 10
Asset Names
[1] "AAPL" "NVDA" "MSFT" "AXP" "WMT" "BAC" "GS" "MS" "DIS" "XOM"
Constraints
Enabled constraint types
- full_investment
- long_only
Objectives:
Enabled objective names
- mean
Now I defined the investment constrains for our investments, which is based on our task a full investment constrained, due to the fact that we would like to be fully invested the whole period. The long only constrain comes from the idea that we are just caring about the upper part of the efficient frontier, in order to construct the tangency portfolio, which is the most efficient way to set our 10 stocks together concerning the faced risked. So, in order words the tangency portfolio maximizes the Sharpe Ratio. The second portfolio we have to construct is the Minimum-Variance Portfolio. The “third” portfolio would be an absolute passive strategy we would just invest in the SP500.
opt_port <- optimize.portfolio(R=stock_returns_xts, portfolio=port,
optimize_method="ROI", trace=TRUE)
opt_port
***********************************
PortfolioAnalytics Optimization
***********************************
Call:
optimize.portfolio(R = stock_returns_xts, portfolio = port, optimize_method = "ROI",
trace = TRUE)
Optimal Weights:
AAPL NVDA MSFT AXP WMT BAC GS MS DIS XOM
0 1 0 0 0 0 0 0 0 0
Objective Measure:
mean
0.03487
So, as we see we would make the most money if we would just everything in Nividia. However, we do not look for the maximum return, we look for the maximum return in relation to the taken risk.
plot(opt_port, chart.assets=TRUE, main="Maximum Return",
xlim=c(0,0.3), ylim=c(0,0.5))
chart.RiskReward(opt_port,return.col="mean", risk.col="sd",
chart.assets=TRUE,
xlim=c(0, 0.25),
main="Maximum Return")
frontier <- create.EfficientFrontier(R=stock_returns_xts,
portfolio=port,
type="mean-StdDev")
Registered S3 method overwritten by 'ROI':
method from
print.constraint PortfolioAnalytics
executing %dopar% sequentially: no parallel backend registered
chart.EfficientFrontier(frontier, match.col="StdDev", type="l",rf = 0, tangent.line = TRUE, chart.assets = TRUE,)
The efficient frontier is the set of optimal portfolios that offer the highest expected return for a defined level of risk or the lowest risk for a given level of expected return. Portfolios that lie below the efficient frontier are sub-optimal because they do not provide enough return for the level of risk.
port_tan <- portfolio.spec(assets=stock_names) %>%
add.constraint(type="full_investment") %>%
add.constraint(type = "long_only") %>%
add.objective(type="return", name="mean")
port_tan
**************************************************
PortfolioAnalytics Portfolio Specification
**************************************************
Call:
portfolio.spec(assets = stock_names)
Number of assets: 10
Asset Names
[1] "AAPL" "NVDA" "MSFT" "AXP" "WMT" "BAC" "GS" "MS" "DIS" "XOM"
Constraints
Enabled constraint types
- full_investment
- long_only
Objectives:
Enabled objective names
- mean
init.portf <- portfolio.spec(assets=stock_names)
init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
init.portf <- add.constraint(portfolio=init.portf, type="long_only")
init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
init.portf
**************************************************
PortfolioAnalytics Portfolio Specification
**************************************************
Call:
portfolio.spec(assets = stock_names)
Number of assets: 10
Asset Names
[1] "AAPL" "NVDA" "MSFT" "AXP" "WMT" "BAC" "GS" "MS" "DIS" "XOM"
Constraints
Enabled constraint types
- full_investment
- long_only
Objectives:
Enabled objective names
- mean
- StdDev
Maximizing the Sharpe Ratio can be formulated as a quadratic programming problem and solved very quickly using optimize_method=“ROI”. Although “StdDev” was specified as an objective, the quadratic programming problem uses the variance-covariance matrix in the objective function.
The default actin if “mean” and “StdDev” are specified as objectives with optimize_method=“ROI” is to maximize quadratic utility. If we want to maximize Sharpe Ratio, we need to pass in maxSR=TRUE to optimize the portfolio, which should lead us to the tangency portfolio.
maxSR.lo.ROI <- optimize.portfolio(R=stock_returns_xts, portfolio=init.portf, optimize_methode="ROI", maxSR=TRUE, trace=TRUE, search_size= 1000000)
Leverage constraint min_sum and max_sum are restrictive,
consider relaxing. e.g. 'full_investment' constraint should be min_sum=0.99 and max_sum=1.01
Iteration: 1 bestvalit: 0.035713 bestmemit: 0.000000 0.074000 0.000000 0.094000 0.498000 0.010000 0.024000 0.000000 0.004000 0.296000
Iteration: 2 bestvalit: 0.035713 bestmemit: 0.000000 0.074000 0.000000 0.094000 0.498000 0.010000 0.024000 0.000000 0.004000 0.296000
Iteration: 3 bestvalit: 0.035713 bestmemit: 0.000000 0.074000 0.000000 0.094000 0.498000 0.010000 0.024000 0.000000 0.004000 0.296000
Iteration: 4 bestvalit: 0.035713 bestmemit: 0.000000 0.074000 0.000000 0.094000 0.498000 0.010000 0.024000 0.000000 0.004000 0.296000
Iteration: 5 bestvalit: 0.035713 bestmemit: 0.000000 0.074000 0.000000 0.094000 0.498000 0.010000 0.024000 0.000000 0.004000 0.296000
Iteration: 6 bestvalit: 0.035190 bestmemit: 0.000000 0.002000 0.078000 0.014000 0.518000 0.000000 0.000000 0.010000 0.000000 0.378000
Iteration: 7 bestvalit: 0.035190 bestmemit: 0.000000 0.002000 0.078000 0.014000 0.518000 0.000000 0.000000 0.010000 0.000000 0.378000
Iteration: 8 bestvalit: 0.035190 bestmemit: 0.000000 0.002000 0.078000 0.014000 0.518000 0.000000 0.000000 0.010000 0.000000 0.378000
Iteration: 9 bestvalit: 0.035190 bestmemit: 0.000000 0.002000 0.078000 0.014000 0.518000 0.000000 0.000000 0.010000 0.000000 0.378000
Iteration: 10 bestvalit: 0.035190 bestmemit: 0.000000 0.002000 0.078000 0.014000 0.518000 0.000000 0.000000 0.010000 0.000000 0.378000
Iteration: 11 bestvalit: 0.035190 bestmemit: 0.000000 0.002000 0.078000 0.014000 0.518000 0.000000 0.000000 0.010000 0.000000 0.378000
Iteration: 12 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 13 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 14 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 15 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 16 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 17 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 18 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 19 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 20 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 21 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 22 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 23 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 24 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 25 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 26 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 27 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
Iteration: 28 bestvalit: 0.033710 bestmemit: 0.104000 0.002000 0.120000 0.002000 0.444000 0.006000 0.026000 0.000000 0.094000 0.202000
[1] 0.104 0.002 0.120 0.002 0.444 0.006 0.026 0.000 0.094 0.202
maxSR.lo.ROI
***********************************
PortfolioAnalytics Optimization
***********************************
Call:
optimize.portfolio(R = stock_returns_xts, portfolio = init.portf,
search_size = 1e+06, trace = TRUE, optimize_methode = "ROI",
maxSR = TRUE)
Optimal Weights:
AAPL NVDA MSFT AXP WMT BAC GS MS DIS XOM
0.104 0.002 0.120 0.002 0.444 0.006 0.026 0.000 0.094 0.202
Objective Measures:
mean
0.008992
StdDev
0.0427
It needed 17 Iterations to come as close as possible to the tangeny portfolio. We would put a big stake into Walmart, Exxon-Mobil, Disney. Although the maximum Sharpe Ratio objective can be solved quickly and accurately with optimize_method=“ROI”, it is also possible or DEoptim. These solvers have the added flexibility of using different methods to calculate the Sharpe Ratio (e.g. we could specify annualized measures of risk and return).
maxSR.lo.RP <- optimize.portfolio(R=stock_returns_xts, portfolio=init.portf, optimize_method="random", search_size=20000, trace=TRUE)
Leverage constraint min_sum and max_sum are restrictive,
consider relaxing. e.g. 'full_investment' constraint should be min_sum=0.99 and max_sum=1.01
maxSR.lo.RP
***********************************
PortfolioAnalytics Optimization
***********************************
Call:
optimize.portfolio(R = stock_returns_xts, portfolio = init.portf,
optimize_method = "random", search_size = 20000, trace = TRUE)
Optimal Weights:
AAPL NVDA MSFT AXP WMT BAC GS MS DIS XOM
0.120 0.000 0.000 0.018 0.416 0.000 0.000 0.046 0.060 0.340
Objective Measures:
mean
0.008335
StdDev
0.04209
chart.RiskReward(maxSR.lo.RP, risk.col="StdDev", return.col="mean")
maxSR.lo.DE <- optimize.portfolio(R=stock_returns_xts, portfolio=init.portf, optimize_method="DEoptim", search_size=20000, trace=TRUE)
Leverage constraint min_sum and max_sum are restrictive,
consider relaxing. e.g. 'full_investment' constraint should be min_sum=0.99 and max_sum=1.01
Iteration: 1 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 2 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 3 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 4 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 5 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 6 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 7 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 8 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 9 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 10 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 11 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 12 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 13 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 14 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 15 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
Iteration: 16 bestvalit: 0.035457 bestmemit: 0.002000 0.062000 0.006000 0.000000 0.456000 0.078000 0.018000 0.000000 0.032000 0.346000
[1] 0.002 0.062 0.006 0.000 0.456 0.078 0.018 0.000 0.032 0.346
maxSR.lo.DE
***********************************
PortfolioAnalytics Optimization
***********************************
Call:
optimize.portfolio(R = stock_returns_xts, portfolio = init.portf,
optimize_method = "DEoptim", search_size = 20000, trace = TRUE)
Optimal Weights:
AAPL NVDA MSFT AXP WMT BAC GS MS DIS XOM
0.002 0.062 0.006 0.000 0.456 0.078 0.018 0.000 0.032 0.346
Objective Measures:
mean
0.007762
StdDev
0.04322
chart.RiskReward(maxSR.lo.DE, risk.col="StdDev", return.col="mean")
The Minmimum Variance Portfolio minimizing the variance of the portfolio
port_minvar <- portfolio.spec(assets=stock_names) %>%
add.constraint(type="full_investment") %>%
add.constraint(type = "long_only") %>%
add.objective(type="risk", name="var")
port_minvar
**************************************************
PortfolioAnalytics Portfolio Specification
**************************************************
Call:
portfolio.spec(assets = stock_names)
Number of assets: 10
Asset Names
[1] "AAPL" "NVDA" "MSFT" "AXP" "WMT" "BAC" "GS" "MS" "DIS" "XOM"
Constraints
Enabled constraint types
- full_investment
- long_only
Objectives:
Enabled objective names
- var
opt_minvar <- optimize.portfolio(R = stock_returns_xts,portfolio = port_minvar,
optimize_method = "ROI", trace = TRUE, search_size= 1000000)
Registered S3 method overwritten by 'ROI':
method from
print.constraint PortfolioAnalytics
opt_minvar
***********************************
PortfolioAnalytics Optimization
***********************************
Call:
optimize.portfolio(R = stock_returns_xts, portfolio = port_minvar,
optimize_method = "ROI", search_size = 1e+06, trace = TRUE)
Optimal Weights:
AAPL NVDA MSFT AXP WMT BAC GS MS DIS XOM
0.0241 0.0000 0.0615 0.0000 0.4551 0.0000 0.0000 0.0000 0.1094 0.3499
Objective Measure:
StdDev
0.04021
So, as we can see we see that we would invest the majority of our capital in Walmart and Exxon Mobile, due to the low standard deviation and quiet interesting we would not invest in Nividia.
chart.RiskReward(opt_minvar,return.col="mean", risk.col="sd",
chart.assets=TRUE,
xlim=c(0.01, 0.25),
main="Minimum Variance")
wts_tan <- c(0.104, 0.002, 0.120, 0.002, 0.444, 0.006, 0.026, 0.000, 0.094, 0.202)
wts_minvar <- c(0.0241, 0.0000, 0.0615, 0.0000, 0.4551, 0.0000, 0.0000, 0.0000, 0.1094, 0.3499)# Weights from the Minimum Variance Portfolio
Portfolio Returns
stock_returns
tan_port <- stock_returns %>%
tq_portfolio(assets_col = symbol,
returns_col = monthly.returns,
weights = wts_tan,
col_rename = "Return")
tan_port
minvar_port <- stock_returns %>%
tq_portfolio(assets_col = symbol,
returns_col = monthly.returns,
weights = wts_minvar,
col_rename = "Return")
minvar_port
For this task we take the SP500 as the benchmark.
bench_returns <- "^GSPC" %>%
tq_get(get = "stock.prices",
from = "2000-01-01",
to = "2020-11-17") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Benchmark")
bench_returns
ov_tan_port <- left_join(tan_port,bench_returns,by = "date")
ov_minvar_port <- left_join(minvar_port,bench_returns,by = "date")
ov_tan_port
ov_minvar_port
ov_tan_port %>%
tq_performance(Ra = Return, Rb = Benchmark,
performance_fun = table.CAPM)
ov_minvar_port %>%
tq_performance(Ra = Return, Rb = Benchmark,
performance_fun = table.CAPM)
sharp_tan <- (0.1112-0)/sd(ov_tan_port$Return)
sharp_minvar <- (0.0605-0)/sd(ov_minvar_port$Return)
sharp_bench <- 1 #Checked it on the internet on an average base it is close to 1
ov_sharp <- cbind(sharp_tan, sharp_minvar, sharp_bench)
ov_sharp
sharp_tan sharp_minvar sharp_bench
[1,] 2.020902 1.400214 1
tan_r <- geometric.mean(ov_tan_port$Return)
NaNs producedWarning messages:
1: replacing previous import ‘data.table::last’ by ‘xts::last’ when loading ‘FactorAnalytics’
2: replacing previous import ‘data.table::first’ by ‘xts::first’ when loading ‘FactorAnalytics’
3: replacing previous import ‘data.table::melt’ by ‘reshape2::melt’ when loading ‘FactorAnalytics’
minvar_r <- geometric.mean(ov_minvar_port$Return)
NaNs produced
bench_r <- geometric.mean(ov_minvar_port$Benchmark)
NaNs produced
FV_tan <- FV(rate = tan_r, nper = 1:239,pv = 100,pmt = -100,type = 1)
FV_minvar <- FV(rate = minvar_r, nper = 1:239, pv = 100, pmt = -100, type=1)
FV_bench <- FV(rate=bench_r, nper = 1:239, pv = 100, pmt = -100, type = 1)
id <- c(1:239)
as.data.frame(FV_tan)
as.data.frame(id)
as.data.frame(FV_minvar)
as.data.frame(FV_bench)
ov_list <- cbind(id, FV_tan,FV_minvar,FV_bench)
ov_list <- as.data.frame(ov_list)
invest <- ov_list[239,]
invest
ggplot(ov_list, aes(x=id)) +
geom_line(aes(y = FV_tan), color = "darkred") +
geom_line(aes(y = FV_minvar), color="steelblue", linetype="twodash") +
geom_line(aes(y=FV_bench), color="pink") +
ggtitle("Wealth Development")
The tangency portfolio performed very good related to the others.
PV_tan <- PV(rate = tan_r,nper = 1:239,fv =-invest$FV_tan ,pmt = 0,type = 1)
PV_minvar <- PV(rate =minvar_r, nper=1:239,fv=-invest$FV_minvar,pmt=0,type=1)
PV_bench <- PV(rate=bench_r, nper=1:239, fv=-invest$FV_bench,pmt=0, type=1)
id <- c(1:239)
as.data.frame(PV_tan)
as.data.frame(id)
as.data.frame(PV_minvar)
as.data.frame(PV_bench)
ov_list <- cbind(id, PV_tan,PV_minvar,PV_bench)
ov_list <- as.data.frame(ov_list)
invest <- ov_list[239,]
invest
As we see if we would like to make an one time investment, but also the exact same amount of money as if we would invest 100 dollar per month. For the Tangency Portfolio we would need to invest 2896.20$ for the Minimium Variance Portfolio we would need to invest 4695.7$ and for the passive strategy S&P500 we would need 5082.05. So, as we can see that the intrest of intrest effect takes here part and dramatically increases the future value of our investment.
ggplot(ov_list, aes(x=id)) +
geom_line(aes(y = PV_tan), color = "darkred") +
geom_line(aes(y = PV_minvar), color="steelblue", linetype="twodash") +
geom_line(aes(y=PV_bench), color="pink") +
ggtitle("One time investment")
For the same two portfolios and the appropriate benchmark calculate overall performance measures (Sharpe ratio, M2 [assume a risk-fre rate of 0], Treynor Ratio, Jensen’s Alpha and Information ratio). Interpret. Additional do the two market timing regressions (ch 24.4) and see whether your portfolios can “time” the market.
For the Evaluation check the solution in excercise 2 there you can find the CAPM Table, which shows us the Sharpe Ratio, Treynor Ratio, Alpha and the Information Ratio.
Timing Regressions Treynor and Mazuy = TM
ts_ov_tan_port <- ts(ov_tan_port)
ts_ov_minvar_port <- ts(ov_minvar_port)
MarketTiming(Ra = ts_ov_tan_port[,2],Rb = ts_ov_tan_port[,3],Rf = 0,method = "TM")
Alpha Beta Gamma
to 0.006352555 0.9523198 1.263177
MarketTiming(Ra = ts_ov_minvar_port[,2],Rb = ts_ov_minvar_port[,3],Rf = 0,method = "TM")
Alpha Beta Gamma
to 0.003237624 0.7565239 0.8547895
If the gamma coefficient in the regression is positive, then the estimated equation describes a convex upward-sloping regression “line”. Gamma is a measure of the curvature of the regression line. If gamma is positive as the Tangency Portfolio clearly is it indicates that the manager’s investment strategy demonstrates market timing ability.
Work through trough the demo demo(relative_ranking). Use what you learn here, form an appropriate opinion on the ranking of your assets and optimize a Minimum Variance and Maximum Sharpe ratio Portfolio. Which one performs better?
demo(relative_ranking)
data(edhec)
R <- edhec[,1:4]
funds <- colnames(R)
#’ Construct initial portfolio with basic constraints.
init.portf <- portfolio.spec(assets=funds)
init.portf <- add.constraint(portfolio=init.portf, type="weight_sum",
min_sum=0.99, max_sum=1.01)
init.portf <- add.constraint(portfolio=init.portf, type="box",min=0.05, max=0.5)
init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
init.portf
#’ Here we express views on the relative rank of the asset returns.
asset.rank <- c(2, 3, 1, 4)
Use Meucci Fully Flexible Views framework to express views on the relative order of asset returns. Define prior probabilities.
p <- rep(1 / nrow(R), nrow(R))
Express view on the relative ordering of asset returns
m.moments <- meucci.ranking(R, p, asset.rank)
Express views using the method described in Almgren and Chriss, “Portfolios from Sorts”.
ac.moments <- list()
ac.moments
ac.moments$mu <- ac.ranking(R, asset.rank)
Sample estimate for second moment
ac.moments$sigma <- cov(R)
Generate random portfolios for use in the optimization.
rp <- random_portfolios(init.portf, 5000)
rp
Run the optimization using first and second moments estimated from Meucci’s Fully Flexible Views framework using the moments we calculated
opt.meucci <- optimize.portfolio(R,
init.portf,
optimize_method="random",
rp=rp,
trace=TRUE,
momentargs=m.moments)
opt.meucci
Run the optimization using first moment estimated based on Almgren and Chriss, “Portfolios from Sorts”. The second moment uses the sample estimate.
opt.ac <- optimize.portfolio(R,
init.portf,
optimize_method="random",
rp=rp,
trace=TRUE,
momentargs=ac.moments)
opt.ac
For comparison, run the optimization using sample estimates for first and second moments.
opt.sample <- optimize.portfolio(R,
init.portf,
optimize_method="random",
rp=rp,
trace=TRUE)
opt.sample
Here we plot the optimal weights of each optimization.
chart.Weights(combine.optimizations(list(meucci=opt.meucci,
ac=opt.ac,
sample=opt.sample)),
ylim=c(0,1), plot.type="barplot")
Here we define a custom moment function to estimate moments based on relative ranking views. Asset are ranked according to a momentum or reversal view based on the previous n periods.
moment.ranking <- function(R, n=1, momentum=TRUE, method=c("meucci", "ac")){
# Moment function to estimate moments based on relative ranking of
# expected returns.
method <- match.arg(method)
# Use the most recent n periods of returns
tmpR <- apply(tail(R, n), 2, function(x) prod(1 + x) - 1)
if(momentum){
# Assume that the assets with the highest return will continue to outperform
asset.rank <- order(tmpR)
} else {
# Assume that the assets with the highest return will reverse
asset.rank <- rev(order(tmpR))
}
switch(method,
meucci = {
# Meucci Fully Flexible Views framework
# Prior probabilities
p <- rep(1 / nrow(R), nrow(R))
# Relative ordering view
moments <- meucci.ranking(R, p, asset.rank)
},
ac = {
# Almgren and Chriss Portfolios from Sorts
moments <- list()
moments$mu <- ac.ranking(R, asset.rank)
# Sample estimate for second moment
moments$sigma <- cov(R)
}
)
return(moments)
}
opt.bt.meucci
Here we run out of sample backtests to test the out of sample performance using the different frameworks to express our views on relative asset return ranking.
pt.bt.meucci <- optimize.portfolio.rebalancing(R, init.portf,
optimize_method="random",
rebalance_on="quarters",
training_period=100,
rp=rp,
momentFUN="moment.ranking",
n=2,
momentum=TRUE,
method="meucci")
pt.bt.meucci
opt.bt.ac <- optimize.portfolio.rebalancing(R, init.portf,
optimize_method="random",
rebalance_on="quarters",
rp=rp,
momentFUN="moment.ranking",
n=2,
momentum=TRUE,
method="ac")
opt.bt.sample <- optimize.portfolio.rebalancing(R, init.portf,
optimize_method="random",
rebalance_on="quarters",
training_period=100,
rp=rp)
Compute returns and chart performance summary.
ret.meucci <- Return.portfolio(R, extractWeights(opt.bt.meucci))
ret.ac <- Return.portfolio(R, extractWeights(opt.bt.ac))
ret.sample <- Return.portfolio(R, extractWeights(opt.bt.sample))
ret <- cbind(ret.meucci, ret.ac, ret.sample)
colnames(ret) <- c("meucci.rank", "ac.rank", "sample")
charts.PerformanceSummary(ret, main="Ranking Views Performance")